package d1_arrays;

import java.util.Arrays;
import java.util.Comparator;

public class ArraysDemo02 {
    public static void main(String[] args) {
        //目标:给存储对象的数组进行排序
        Student[] students = new Student[4];
        //进行数据封装
        students[0] = new Student("周芷若", 21, '女', 169.3);
        students[1] = new Student("殷素素", 38, '女', 172.2);
        students[2] = new Student("小昭", 19, '女', 168.5);
        students[3] = new Student("张无忌", 23, '男', 183.5);

        //自定义排序规则方式一: 让对象所在的类实现比较规则接口Comparable, 重写compareTo方法, 来指定比较规则
        Arrays.sort(students);//sort底层会默认,用数组的第一个对象调用类重写的compareTo方法,跟第二个对象进行比较

        //自定义比较规则二: sort存在重载方法, 支持自带Comparator比较器对象来直接指定比较规则(优先)
        // public static <T> void sort(T[] a, Comparator<? super T> c)
        Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                //return o1.getAge() - o2.getAge();//升序

                //如果向按照double的数据进行排序
                //1、老老实实进行判断书写
                /*if (o1.getHeight() > o2.getHeight()){
                    return 1;
                } else if (o1.getHeight() < o2.getHeight()) {
                    return -1;
                }
                return 0;*/
                //2、包装类Double对上面的逻辑进行了封装
                return Double.compare(o1.getHeight(), o2.getHeight());
            }
        });

        System.out.println(Arrays.toString(students));
    }
}
